<template>
  <div class="section">
    <div class="user">
      <div>名称</div>
      <div>年龄</div>
    </div>
    <RecycleScroller
      class="scroller"
      :items="list"
      :item-size="32"
      key-field="id"
      v-slot="{ item }"
    >
      <div class="user" @click="add(item)">
        <div>{{ item.name }}</div>
        <div>{{ item.age }}</div>
      </div>
    </RecycleScroller>
  </div>
</template>
   
  <script>
export default {
  name: "vueVirtualScroller",
  data() {
    return {
      list: [
        {
          id: "9999991",
          name: "name" + "9999991",
          age: "age" + "9999991",
        },
        {
          id: "9999992",
          name: "name" + "9999992",
          age: "age" + "9999992",
        },
        {
          id: "9999993",
          name: "name" + "9999993",
          age: "age" + "9999993",
        },
      ],
    };
  },
  computed: {
    // list() {
    //   let arr = [];
    //   for (let i = 1; i <= 10000; i++) {
    //     arr.push({
    //       id:i,
    //       name: "name" + i,
    //       age: "age" + i,
    //     });
    //   }
    //   return arr;
    // },
  },
  methods: {
    // 点击每一行获取数据
    add(item) {
      console.log("item", item);
    },
  },
  mounted() {
    let ele = document.querySelector(".vue-recycle-scroller")  //父元素
    let wrapper = document.querySelector(".vue-recycle-scroller__item-wrapper") //可滚动容器
    setTimeout(()=>{
        for (let i = 1; i <= 10000; i++) {
            this.list.push({
                id: i,
                name: "name" + i,
                age: "age" + i,
            });
        }
        // 在出现滚动条之后加宽容器
        this.$nextTick(()=>{
            console.log(wrapper,wrapper.style['min-height'])
            ele.style.width = 'calc(100% + 17px)'
        })
    },1000)

    // 监听容器大小变化  失败
    wrapper.addEventListener('resize',(e)=>{
        console.log('大小变化了',e)
    })
    // 监听滚动条事件 可以使用
    ele.addEventListener('scroll',()=>{
        console.log('滚动')
    })
  },
};
</script>
   
  <style scoped lang="less">
.section {
  width: 400px;
  & > :nth-child(2) {
    width: 100%;
    border-bottom: 1px solid #ccc;
  }
}
.scroller {
  height: 300px;
}

.user {
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  & > div {
    width: 100%;
    height: 32px;
    line-height: 32px;
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
    text-align: center;
  }
}
</style>